home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / ubppmpqs.zip / ppmp5.asm < prev    next >
Assembly Source File  |  1992-05-10  |  10KB  |  694 lines

  1. ;PPMP5.ASM 2.0
  2. ;    MACHINE LANGUAGE SUBROUTINES
  3. ;    FOR PPMPQS
  4. ;    1991/92 by YUJI KIDA
  5. ;
  6. .386p
  7.  
  8. code    segment    use16
  9.     assume    cs:code,ds:code
  10.  
  11.  
  12.     org    100h
  13. start:
  14.  
  15.     INCLUDE    UBP.MAC
  16.     include    ppmp.h
  17.  
  18.  
  19.     JMP    start0
  20.  
  21.     align    4
  22. GaussLongbase    dd    ?
  23. GaussLongNow    dd    ?
  24. XMEMsize    dd    ?
  25.  
  26. MATbytes    dw    0,0
  27. MATbytesW    dw    0,0
  28. MATbytesW4    dw    0,0
  29. MATwords    dw    0,0
  30. MATwordsW    dw    0,0
  31. MATdwords    dw    0,0
  32. MATdwordsW    dw    0,0
  33.  
  34. makexorDwordsW    dw    0,0
  35. makexorskip    dw    0,0
  36. loadsavebytes    dw    ?
  37.  
  38. unitrows    dw    0
  39. startrow    dw    ?
  40. checkingrow    dw    ?
  41. localrank    dw    ?
  42.  
  43. MATsize        dw    ?
  44.  
  45. handle        dw    ?
  46. anshandle    dw    ?
  47. handle1        dw    ?
  48. size1        dw    ?
  49. handle2        dw    ?
  50.  
  51. RWrow        dw    ?
  52.  
  53. answers        dw    ?
  54.  
  55. spmem        dw    ?
  56.  
  57.  
  58. ;branch
  59.  
  60. START0:
  61.     MOV_AX    AR0        ;ARRAY[0] is the command number
  62.     MOV    BX,OFFSET CMD_TBL
  63.     SHL    AX,1
  64.     ADD    BX,AX
  65.     JMP    CS:[BX]
  66.  
  67. CMD_TBL:
  68.     DW    gauss_INIT,GAUSS,closefiles
  69.  
  70. ;
  71. ; * Close File
  72. ;
  73. ;command#=2
  74.  
  75. closefiles:
  76.     mov    ax,cs
  77.     mov    ds,ax
  78.  
  79.     mov    ah,3eh
  80.     mov    bx,[handle1]
  81.     int    21h
  82.  
  83.     mov    ah,3eh
  84.     mov    bx,[handle2]
  85.     int    21h
  86.  
  87.     mov    ah,3eh
  88.     mov    bx,[anshandle]
  89.     int    21h
  90.  
  91.     mov    ax,[answers]
  92.     mov    bx,AR0
  93.     mov    [bx],ax
  94.     
  95.     mov    ax,ss
  96.     mov    ds,ax
  97.     retf
  98.  
  99.  
  100. ;
  101. ;** Gaussin elimination
  102. ;
  103. ;command#=1
  104.  
  105. Gauss:
  106.     mov    ax,cs
  107.     mov    ds,ax
  108.     mov    es,ax
  109.     mov    [spmem],sp
  110.  
  111.     mov    ax,[startrow]
  112.     mov    [checkingrow],ax
  113.     call    getpivotrows
  114.     or    ax,ax
  115.     jz    short gauss100
  116.     mov    [localrank],ax
  117.     mov    ax,[checkingrow]
  118.     mov    [startrow],ax
  119.     call    erase_below
  120. gauss100:
  121.     mov    ax,[startrow]
  122.     cmp    ax,[MATsize]
  123.     jb    short gaussret
  124.     mov    bx,AR0
  125.     mov    word ptr [bx],0
  126. gaussret:
  127.     mov    ax,ss
  128.     mov    ds,ax
  129.     mov    es,ax
  130.     retf
  131.  
  132.  
  133. getpivotrows:
  134.     mov    ax,[MATbytesW]
  135.     mov    [loadsavebytes],ax
  136.     mov    eax,[GaussLongbase]
  137.     mov    [GaussLongNow],eax
  138.     mov    cx,[unitrows]
  139.     mov    ax,[checkingrow]
  140. getpivot10:
  141.     cmp    ax,[MATsize]
  142.     jae    short getpivot50    ;data ended without filling all
  143.  
  144.     push    cx
  145.     mov    [RWrow],ax
  146.     call    load_row
  147.  
  148.     mov    di,offset BUFFER
  149.     call    calcMSB
  150.     or    ax,ax
  151.     jz    short getpivot20    ;erased data
  152.     mov    [MSBoffset],cx
  153.     mov    [MSBmask],ax
  154.     call    erasebyothers
  155.     jc    short getpivot20    ;there is a vanished data
  156.     call    store2extended
  157.     mov    ax,[checkingrow]
  158.     inc    ax
  159.     mov    [checkingrow],ax
  160.     pop    cx
  161.     loop    getpivot10
  162.  
  163. getpivot50:
  164.     mov    ax,[unitrows]
  165.     sub    ax,cx            ;ax = rank increased now
  166.     ret
  167.  
  168. getpivot20:
  169.     pop    cx
  170.     mov    ax,[checkingrow]
  171.     inc    ax
  172.     mov    [checkingrow],ax
  173.     jmp    getpivot10
  174.  
  175.  
  176. erasemac    macro
  177.     mov    eax,fs:[esi]
  178.     xor    [di],eax
  179.     add    esi,4
  180.     add    di,4
  181. endm
  182.  
  183. erasebyothers:
  184.     mov    esi,[GaussLongBase]
  185.     cmp    esi,[GaussLongNow]
  186.     je    eraseret        ;no former
  187. erase10:
  188.     mov    bx,fs:[esi]        ;word offset of MSB
  189.     mov    ax,fs:[esi+2]        ;bit pattern of word
  190.     mov    di,offset BUFFER
  191.     test    ax,[bx+di]
  192.     jz    erase20
  193.     add    esi,4
  194.  
  195.     mov    cx,[MATdwordsW]        ;=dword full row length
  196.     shr    cx,1
  197.     jnc    short erase15
  198.     erasemac
  199. erase15:
  200.     shr    cx,1
  201.     jnc    short erase16
  202.     erasemac
  203.     erasemac
  204. erase16:
  205.     jcxz    erase18
  206.     align    4
  207. erase17:
  208.     erasemac
  209.     erasemac
  210.     erasemac
  211.     erasemac
  212.     loop    erase17
  213. erase18:
  214.     jmp    short erase25
  215. erase20:
  216.     add    esi,dword ptr [MATbytesW4]
  217. erase25:
  218.     cmp    esi,[GaussLongNow]
  219.     jb    erase10        
  220.  
  221.     mov    di,offset BUFFER
  222.     call    calcMSB
  223.     mov    [di-4],cx
  224.     mov    [di-2],ax
  225.     or    ax,ax
  226.     jz    erasegetans1        ;find an answer
  227.     clc
  228. eraseret:
  229.     ret
  230.  
  231. erasegetans1:                ;if the new member becomes an answer
  232.     mov    ah,40h            ;write
  233.     mov    bx,[ANShandle]
  234.     mov    cx,[MATbytes]
  235.     mov    dx,offset BUFFER
  236.     int    21h
  237.     jc    ansdiskerror
  238.     inc    [answers]
  239.     stc
  240.     jmp    eraseret
  241.  
  242.  
  243. ansdiskerror:
  244.     mov    sp,[spmem]
  245.     mov    bx,AR0
  246.     mov    word ptr [bx],8001h
  247.     jmp    gaussret
  248.  
  249.  
  250. storemac    macro
  251.     lodsd
  252.     mov    fs:[edi],eax
  253.     add    edi,4
  254. endm
  255.  
  256. store2extended:
  257.     mov    si,offset MSBoffset
  258.     mov    edi,[GaussLongNow]
  259.     lodsd                ;for MSB data
  260.     mov    fs:[edi],eax
  261.     add    edi,4
  262.     mov    cx,[MATdwordsW]
  263.     shr    cx,1
  264.     jnc    short store15
  265.     storemac
  266. store15:
  267.     shr    cx,1
  268.     jnc    short store16
  269.     storemac
  270.     storemac
  271. store16:
  272.     jcxz    store18
  273.     align    4
  274. store17:
  275.     storemac
  276.     storemac
  277.     storemac
  278.     storemac
  279.     loop    store17
  280. store18:
  281.     mov    [GaussLongNow],edi
  282.     ret
  283.  
  284.  
  285. erase_below:
  286.     mov    ax,[checkingrow]
  287.     mov    [RWrow],ax
  288.     mov    cx,[MATsize]
  289.     sub    cx,ax
  290.     jz    short erasebelowret
  291.  
  292.     push    cx            ;/*
  293.  
  294.     mov    ebx,[GAUSSlongbase]
  295.     mov    cx,[localrank]
  296.     xor    ax,ax
  297. eraseb10:
  298.     cmp    ax,fs:[ebx]
  299.     jae    short eraseb20
  300.     mov    ax,fs:[ebx]
  301. eraseb20:
  302.     add    ebx,dword ptr [MATbytesW4]
  303.     loop    eraseb10
  304.     shr    ax,2
  305.     inc    ax
  306.     mov    [makexorDwordsW],ax
  307.     shl    ax,2
  308.     mov    [loadsavebytes],ax
  309.     sub    ax,[MATbytesW]
  310.     neg    ax
  311.     mov    [makexorskip],ax
  312.  
  313.     pop    cx            ;*/
  314.  
  315. erasebelowloop:
  316.     push    cx            ;/*
  317.  
  318.     call    load_row
  319.     call    make_xor        ;if zero must save as answer and 
  320.     call    save_row        ;delete from the matrix
  321.  
  322.     pop    cx            ;*/
  323.     inc    [RWrow]
  324.     loop    erasebelowloop
  325. erasebelowret:
  326.     ret
  327.  
  328.  
  329. xormac    macro
  330.     mov    eax,fs:[esi]
  331.     xor    [di],eax
  332.     add    esi,4
  333.     add    di,4
  334. endm
  335.  
  336. make_xor:
  337.     mov    esi,[GaussLongBase]
  338.     mov    cx,[localrank]
  339. makexor10:
  340.     push    cx
  341.     mov    di,word ptr fs:[esi]    ;MSB word adr
  342.     mov    ax,fs:[esi+2]        ;mask
  343.     add    di,offset BUFFER
  344.     test    [di],ax
  345.     jz    makexor60
  346.  
  347.     add    esi,4
  348.     mov    di,offset BUFFER
  349.     mov    cx,[makexordwordsW]
  350.     shr    cx,1
  351.     jnc    short makexor15
  352.     xormac
  353. makexor15:
  354.     shr    cx,1
  355.     jnc    short makexor16
  356.     xormac
  357.     xormac
  358. makexor16:
  359.     jcxz    makexor18
  360.     align    4
  361. makexor17:
  362.     xormac
  363.     xormac
  364.     xormac
  365.     xormac
  366.     loop    makexor17
  367. makexor18:
  368.     add    esi,dword ptr [makexorskip]
  369.     jmp    short makexor70
  370. makexor60:
  371.     add    esi,dword ptr [MATbytesW4]
  372. makexor70:
  373.     pop    cx
  374.     dec    cx
  375.     jnz    makexor10
  376.     ret
  377.  
  378.  
  379. load_row:
  380.     mov    ax,[RWrow]
  381.     
  382.     mov    bx,[handle1]
  383.     xor    dx,dx
  384.     cmp    ax,[size1]
  385.     jb    load_row10
  386.  
  387.     mov    bx,[handle2]
  388.     mov    dx,[size1]
  389. load_row10:
  390.     mov    [handle],bx
  391.  
  392.     sub    ax,dx
  393.     mov    dx,[MATbytesW]
  394.     mul    dx
  395.     mov    cx,dx
  396.     mov    dx,ax            ;cx:dx = file ptr
  397.  
  398.     mov    ah,42h            ;move file ptr
  399.     mov    al,0            ;absolute
  400.     mov    bx,[handle]
  401.     int    21h
  402. load_row20:
  403.     mov    ah,3fh            ;read
  404.     mov    bx,[handle]
  405.     mov    cx,[loadsavebytes]
  406.     mov    dx,offset BUFFER
  407.     int    21h
  408.  
  409.     ret
  410.  
  411.  
  412. save_row:
  413.     mov    ax,[RWrow]
  414.     
  415.     mov    bx,[handle1]
  416.     xor    dx,dx
  417.     cmp    ax,[size1]
  418.     jb    short save_row10
  419.  
  420.     mov    bx,[handle2]
  421.     mov    dx,[size1]
  422. save_row10:
  423.     mov    [handle],bx
  424.  
  425.     sub    ax,dx
  426.     mov    dx,[MATbytesW]
  427.     mul    dx
  428.     mov    cx,dx
  429.     mov    dx,ax            ;cx:dx = file ptr
  430.  
  431.     mov    ah,42h            ;move file ptr
  432.     mov    al,0            ;absolute
  433.     mov    bx,[handle]
  434.     int    21h
  435. save_row20:
  436.     mov    ah,40h            ;write
  437.     mov    bx,[handle]
  438.     mov    cx,[loadsavebytes]
  439.     mov    dx,offset BUFFER
  440.     int    21h
  441.  
  442.     ret
  443.  
  444.  
  445. calcMSB:
  446.     ;in: di = base adr
  447.     ;out: ax = mask of MSB, cx = offset adr of MSB
  448.     ;destroy: nothing
  449.  
  450.     push    di
  451.     add    di,[MATbytesW]
  452.     sub    di,4
  453.     xor    eax,eax
  454.     mov    cx,[MATdwords]
  455.     std
  456.     repe    scasd
  457.     cld
  458.     je    short calcMSBzero    ;ax=cx=0
  459.     add    di,6
  460.     mov    ax,[di]
  461.     or    ax,ax
  462.     jnz    short calcMSB5
  463.     sub    di,2
  464.     mov    ax,[di]
  465. calcMSB5:
  466.     or    ah,ah
  467.     jnz    short calcMSB10        ;if on higher
  468.     mov    ah,al
  469.     bsr    cx,ax
  470.     sub    cx,8
  471.     mov    ax,1
  472.     shl    ax,cl
  473.     mov    cx,di
  474.     pop    di
  475.     sub    cx,di
  476.     ret
  477.  
  478. calcMSB10:
  479.     bsr    cx,ax
  480.     mov    ax,1
  481.     shl    ax,cl
  482.     mov    cx,di
  483.     pop    di
  484.     sub    cx,di
  485.     ret
  486.  
  487. calcMSBzero:                ;if 0 then return with ax=0
  488.     pop    di            ;also cx=0 but has no meaning
  489.     ret
  490.  
  491.     
  492. ;
  493. ;* Initialize for Gaussian elimination
  494. ;
  495. ;command#=0
  496.  
  497. gauss_init:
  498.     ;get parameters
  499.  
  500.     mov    ax,cs
  501.     mov    ds,ax
  502.     mov    es,ax
  503.     mov    [spmem],sp
  504.  
  505.     xor    eax,eax
  506.     mov_ax    AR1
  507.     shl    eax,10
  508.     mov    [XMEMsize],eax    
  509.  
  510.     mov_ax    AR2
  511.     shl    ax,1
  512.     mov    [MATsize],ax
  513.     shr    ax,2            ;* 2 / 8
  514.     mov    [MATbytesW],ax
  515.     add    ax,4
  516.     mov    [MATbytesW4],ax
  517.     sub    ax,4
  518.     shr    ax,1
  519.     mov    [MATbytes],ax
  520.     mov    [MATwordsW],ax
  521.     shr    ax,1
  522.     mov    [MATwords],ax
  523.     mov    [MATdwordsW],ax
  524.     shr    ax,1
  525.     mov    [MATdwords],ax
  526.  
  527.     mov    [startrow],0
  528.     mov    [answers],0
  529.  
  530.     call    setPROTECTfs
  531.  
  532.     mov    eax,GaussAdr
  533.     mov    [GaussLongbase],eax
  534.  
  535.     mov    eax,[XMEMsize]
  536.     add    eax,pmodeworkadr
  537.     sub    eax,[GaussLongbase]
  538.     jb    lackofextendedmemory
  539.     xor    edx,edx            ;eax = size of freearea
  540.     mov    ecx,dword ptr [MATbytesW4]
  541.     div    ecx            ;eax = max number of rows
  542.     movzx    ecx,[MATsize]
  543.     cmp    eax,ecx
  544.     jbe    short checkmemory140
  545.     mov    eax,ecx        
  546. checkmemory140:
  547.     mov    [unitrows],ax
  548.  
  549.     call    openMATRIXfiles
  550.     call    createANSfile
  551.  
  552.     mov    ax,[unitrows]
  553. gauss_init_out:
  554.     mov    bx,AR0
  555.     mov    [bx],ax
  556.  
  557.     mov    ax,ss
  558.     mov    ds,ax
  559.     mov    es,ax
  560.     retf
  561.  
  562. diskerror:
  563.     mov    sp,[spmem]
  564.     mov    ax,8001h    ;set -1
  565.     jmp    gauss_init_out
  566.  
  567. lackofextendedmemory:
  568.     mov    sp,[spmem]
  569.     mov    ax,8002h    ;set -